// Capture a video from camera and save to video file
 // argv[1]: input video file
 // argv[2]: name of new output file

#if 0

#include "cv.h"
#include "highgui.h"

int main( int argc, char* argv[] )
{
	cvNamedWindow("colourImage", CV_WINDOW_AUTOSIZE);
	CvCapture *capture = cvCaptureFromFile("http://192.168.0.52:8080/video"); //= cvCreateFileCapture( argv[1] );

    if (capture == NULL)
        printf("Error opening video stream\n");
    else
        printf("Video stream successfully opened\n");


    cvGrabFrame(capture);
	IplImage *bgr_frame = cvRetrieveFrame(capture); //Init the video read

	// Fixed framerate of 30 Hz assumed
	double fps = 30; //cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);

	CvSize size = cvGetSize(bgr_frame);
	//CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) );


	CvVideoWriter *writer = cvCreateVideoWriter( "out.avi", CV_FOURCC('P','I','M','1'), fps, size ); // MPEG1 codec
	//CvVideoWriter *writer = cvCreateVideoWriter( "out.mjpeg", CV_FOURCC('M','J','P','G'), fps, size );

    if (writer == NULL)
        printf("Error creating video writer\n");
    else
        printf("Video writer successfully created\n");

	IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 3 );


	// Only process 1000 frames
	int count = 0;
	//while( (bgr_frame=cvQueryFrame(capture)) != NULL )
	while( ++count < 500 )
	{
		cvGrabFrame(capture);
		bgr_frame = cvRetrieveFrame(capture);
		//cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
		cvWriteFrame( writer, bgr_frame );

		printf("Frame %d written\n",count);

		cvShowImage("colourImage", bgr_frame);

		cvWaitKey(30);
	}

	cvReleaseVideoWriter( &writer );
	//cvReleaseImage( &logpolar_frame );
	//cvReleaseImage( &bgr_frame );
	cvReleaseCapture( &capture );
	cvDestroyWindow("colourImage");

	return(0);
}

#endif


#if 0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cv.h>
#include <highgui.h>
//#include <cxtypes.h>
#include <cvaux.h>


int main()
{
    int camera_index = 0;
    IplImage *color_frame = NULL;
    int exit_key_press = 0;

    CvCapture *capture = cvCaptureFromFile("http://192.168.0.52:8080/video"); //= cvCreateFileCapture( argv[1] );

	if (capture == NULL)
		printf("Error opening video stream\n");
	else
		printf("Video stream successfully opened\n");


	cvGrabFrame(capture);
	IplImage *bgr_frame = cvRetrieveFrame(capture); //Init the video read

	// Fixed framerate of 10 Hz assumed
	double fps = 30; //cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);

	CvSize size = cvGetSize(bgr_frame);
	//CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) );


	CvVideoWriter *writer = cvCreateVideoWriter( "out.avi", CV_FOURCC('M','J','P','G'), fps, size, 1 ); // MPEG1 codec

    cvNamedWindow("Grayscale video", CV_WINDOW_AUTOSIZE);

    //CvVideoWriter* writer = NULL;
    //writer = cvCreateVideoWriter("out.avi", -1, fps, cvSize((int)cam_w,(int)cam_h), 1);
    if (writer == NULL)
    {
        printf("!!! ERROR: cvCreateVideoWriter\n");
        return -1;
    }

    //while (exit_key_press != 'q')
    for(int i=0; i<200; i++)
    {
        color_frame = cvQueryFrame(capture);
        if (color_frame == NULL)
        {
            printf("!!! ERROR: cvQueryFrame\n");
            break;
        }

        IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
        if (gray_frame == NULL)
        {
            printf("!!! ERROR: cvCreateImage\n");
            continue;
        }

        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvShowImage("Grayscale video", gray_frame);

        cvWriteFrame(writer, gray_frame);

        cvReleaseImage(&gray_frame);

        exit_key_press = cvWaitKey(1);
    }

    cvReleaseVideoWriter(&writer);
    cvDestroyWindow("Grayscale video");
    cvReleaseCapture(&capture);

    return 0;
}
#endif
